﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Zsg_PeerToPeer
{
    public partial class FrmServer : Form
    {
        ServerListener mainListener;
        public FrmServer()
        {
            InitializeComponent();
            start();
            Btn_Start.Enabled = false;
        }

        private void Btn_Start_Click(object sender, EventArgs e)
        {
            start();
            Btn_Start.Enabled = false;
        }
        public void start()
        {
            IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 19931);
            mainListener=  new ServerListener(iPEndPoint);
            mainListener.ReceivedMsg += MainListener_ReceivedMsg;
            mainListener.Start();
        }

        private void MainListener_ReceivedMsg(object sender, string e)
        {
            PushMsg(e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Msg"></param>
        private void PushMsg(string Msg)
        {
            this.BeginInvoke(new Action(() =>
            {
                Rtb_Msg.AppendText(Msg);
            }));
        }

        private void Btn_Close_Click(object sender, EventArgs e)
        {
            if (mainListener!=null)
            {
                mainListener.Close();
            }
            Btn_Start.Enabled = true;
        }
    }
}
